package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgAdvanceInputViewHelper;

/**
 * 
 * 进入待办受理录入结果页面Controller
 * 
 * @author Administrator
 * 
 */

public class TransferRgAdvanceInputResultController extends TmsController {
	private Logger logger = Logger
			.getLogger(TransferRgAdvanceInputResultController.class);

	private static final String SYSPATH = "sysPath";//

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			TransferRgAdvanceInputViewHelper view = (TransferRgAdvanceInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			TransferPersonalCarInfo person = new TransferPersonalCarInfo();
			
			Car car = new Car();
			car = car.findCarBySerial(view.getBasicCarId());
			TmsUserProfile profile = uc.getUserProfile();
			//更新库存车辆信息表（tms_market_car_info车辆业务状态（status）为“5：过户中（补充信息）”
			if(!StringUtil.isNullOrBlank(car.getSerial())){
				car = setEntityFromViewhelper(car, view);
				car.setUpdateUserId(profile.getUniqueId());
				car.setStatus(5);
				// 更新库存表
				car.updateMarketInfoByLicensePlate(car);
			}
			// 跟新过户表
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.getAcceptInfoBySerial(view.getSerial(),"");
			accept.setSerial(view.getSerial());
			accept.setVin(view.getVin());
			accept.setAgentName(view.getAgentName());
			accept.setAgentPhone(view.getAgentPhone());
			accept.setAgentOwnerIdCardNo(view.getAgentOwnerIdCardNo());
			accept.setAgentAddress(view.getAgentAddress());
			accept.setMaterialImages1(view.getMaterialImages1());
			accept.setMaterialImages2(view.getMaterialImages2());
			accept.setMaterialImages3(view.getMaterialImages3());
			accept.setMaterialImages4(view.getMaterialImages4());
			accept.setMaterialImages5(view.getMaterialImages5());
			accept.setOwnersGroupPhoto(view.getOwnersGroupPhoto());
			accept.setStatus(6);
			accept.setUpdateUserId(profile.getUniqueId());
			String sysPath = (String) uc.getAttribute(SYSPATH);
			accept.setPreTransfer(1);
			if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
			{
				accept.setTransferPrice(Integer.parseInt(StringUtil.removeDH(view.getTransferPrice())));
			}else{
				accept.setTransferPrice(0);
			}
			accept.setTaxNo(view.getTaxNo());
			accept.setOwnerType(Integer.parseInt(view.getOwnerType()));
			accept.setMemberId(view.getMemberId());
			accept.updateAcceptInfoAtAdvance(accept, sysPath);

			//add by huangheli at 20150917 start
			//非库存车辆信息表（tms_transfer_personalCar_info）车辆业务状态（status）为“5：过户中（补充信息”
			person = person.getPersonalCarInfoBySerial(accept.getBasicCarId());
			if(person!=null){
				person = setEntityFromViewhelper(person, view);
				person.setUpdateUserId(profile.getUniqueId());
				person.setStatus(5);
				// 更新库存表
				person.updateMarketInfoByLicensePlate(person);
			}
			//add by huangheli at 20150917 end
			
			// 历史记录
			TransferProcessHistory history = new TransferProcessHistory();
			history.setSerial(view.getSerial());
			history.setBusinessType(37);
			history.setInsertUserId(profile.getUniqueId());
			history.setReason("");
			history.insertHisInfoAtAssess(history);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 
	 * 将viewhelp中值set到entity中
	 * 
	 * @throws TmsDaoException
	 */
	private Car setEntityFromViewhelper(Car entity, TransferRgAdvanceInputViewHelper view)
			throws TmsDaoException {
		entity.setSerial(view.getBasicCarId());
		entity.setCarName(view.getCarName());
		entity.setEngine(view.getEngine());
		entity.setLicensePlate(view.getLicensePlate());
		if(view.getColor()!=null && !"".equals(view.getColor()))
		{
			entity.setColor(Integer.parseInt(view.getColor()));
		}else{
			entity.setColor(0);
		}
		
		entity.setRegDate(view.getRegDate());
		entity.setVendorCode(view.getVendorCode());
		entity.setModelCode(view.getModelCode());

		entity.setCarUse(Integer.parseInt(view.getCarUse()));
		entity.setCarType(Integer.parseInt(view.getCarType()));
		entity.setSendCardDate(view.getSendCardDate());
		entity.setCardType(Integer.parseInt(view.getCardType()));
		entity.setRegisterCardNo(view.getRegisterCardNo());
		entity.setCountryStyle(Integer.parseInt(view.getCountryStyle()));
		entity.setCompanyContactPerson(view.getCompanyContactPerson());

		entity.setOwnerType(Integer.parseInt(view.getOwnerType()));
		entity.setCarOwnerIdCardNo(view.getCarOwnerIdCardNo());
		entity.setOwnerOrgNo(view.getOwnerOrgNo());

		entity.setOwnerPhone(view.getOwnerPhone());
		entity.setOwnerAddress(view.getOwnerAddress());
		entity.setCarOwner(view.getCarOwner());
		entity.setBrandId(view.getBrandId());
		entity.setCarStyleId(view.getCarStyleId());
		entity.setVendorId(view.getVendorId());

		entity.setMemberId(Integer.parseInt(view.getMemberId()));

		return entity;
	}
	/**
	 * 
	 * 将viewhelp中值set到entity中
	 * 
	 * @throws TmsDaoException
	 */
	private TransferPersonalCarInfo setEntityFromViewhelper(TransferPersonalCarInfo entity, TransferRgAdvanceInputViewHelper view)
			throws TmsDaoException {
		entity.setSerial(view.getBasicCarId());
		entity.setCarName(view.getCarName());
		entity.setEngine(view.getEngine());
		entity.setLicensePlate(view.getLicensePlate());
		if(view.getColor()!=null && !"".equals(view.getColor()))
		{
			entity.setColor(Integer.parseInt(view.getColor()));
		}else{
			entity.setColor(0);
		}
		
		entity.setRegDate(view.getRegDate());
		entity.setVendorCode(view.getVendorCode());
		entity.setModelCode(view.getModelCode());

		entity.setCarUse(Integer.parseInt(view.getCarUse()));
		entity.setCarType(Integer.parseInt(view.getCarType()));
		entity.setSendCardDate(view.getSendCardDate());
		entity.setCardType(Integer.parseInt(view.getCardType()));
		entity.setRegisterCardNo(view.getRegisterCardNo());
		entity.setCountryStyle(Integer.parseInt(view.getCountryStyle()));
		entity.setCompanyContactPerson(view.getCompanyContactPerson());

		entity.setOwnerType(Integer.parseInt(view.getOwnerType()));
		entity.setCarOwnerIdCardNo(view.getCarOwnerIdCardNo());
		entity.setOwnerOrgNo(view.getOwnerOrgNo());

		entity.setOwnerPhone(view.getOwnerPhone());
		entity.setOwnerAddress(view.getOwnerAddress());
		entity.setCarOwner(view.getCarOwner());
		entity.setBrandId(view.getBrandId());
		entity.setCarStyleId(view.getCarStyleId());
		entity.setVendorId(view.getVendorId());

		return entity;
	}
}
